/*
 * Copyright (c) 2006, 2022, webrx.cn All rights reserved.
 *
 */
package cn.webrx;

import com.mysql.cj.xdevapi.Type;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLType;
import java.sql.Types;

/**
 * <p>Project: jdbc202202 - CallableStatementDemo
 * <p>Powered by webrx On 2022-08-22 14:23:27
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
public class CallableStatementDemo {
    @Test @DisplayName("测试调用执行存储过程对象")
    void cstdemo(){
        try{
            //建立数据库连接
            Connection conn = DriverManager.getConnection("jdbc:mysql:/db?user=root");
            var cst = conn.prepareCall("{call p_updatebyid(?,?,?)}");
            cst.setLong(1,99926658374434824L);
            cst.setString(3,"wangq");
            cst.setDouble(2,55.56);
            cst.executeUpdate();

            //根据连接建立CallableStatement语句对象，用来
            //CallableStatement cst = conn.prepareCall("call p_show");
            //CallableStatement cst = conn.prepareCall("{call p_show}");
            //CallableStatement cst = conn.prepareCall("{call p_show()}");
            //ResultSet rs = cst.executeQuery();
            //System.out.println(rs.isBeforeFirst());
            //while(rs.next()){
            //    System.out.println(rs.getLong("id"));
            //    System.out.println(rs.getString("name"));
            //    System.out.println(rs.getString("author"));
            //}
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    @Test @DisplayName("测试调用执行存储过程对象")
    void cstdemo2(){
        try{
            //建立数据库连接
            Connection conn = DriverManager.getConnection("jdbc:mysql:/db?user=root");
            var cst = conn.prepareCall("{call p_count(?)}");
            cst.registerOutParameter(1, Types.INTEGER);
            cst.execute();
            System.out.println(cst.getInt(1));
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}
